package com.gmp.thewizard;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class GameActivity extends Activity {
	
	protected static final int LOGLEVEL = 0;
	protected static final boolean WARN = LOGLEVEL > 1;
	protected static final boolean DEBUG = LOGLEVEL > 0;
	private static final String TAG = "GameActivity";
	protected SharedPreferences settings;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.settings = getSharedPreferences("THEWIZARD", MODE_PRIVATE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
		
		Log.d(TAG,"Starting: "+this.settings.getBoolean("INTRO", true));
		
		if(this.settings.getBoolean("INTRO", true)){
			DialogFragment fragment = IntroAlertFragment.newInstance(R.string.intro_dia_title);
			fragment.show(this.getFragmentManager(), "intro");
			this.settings.edit().putBoolean("INTRO", false).apply();
		} else
			setContentView(new Panel(this));
	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_surface, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
			case R.id.menu_quit:
				this.finish();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}
	
	protected void OnPause(){
		super.onPause();
		View panel = findViewById(R.id.panel_view_id);
		if(panel instanceof Panel)
			if(!((Panel) panel).stopCountDown())
				System.out.println("pause: COUNTER FAILED!");
	}
	
	protected void onStop(){
		super.onStop();
		View panel = findViewById(R.id.panel_view_id);
		if(panel instanceof Panel)
			if(!((Panel) panel).stopCountDown())
				System.out.println("stop: COUNTER FAILED!");
	}
	
	protected void reload(){
		Intent intent = getIntent();
		overridePendingTransition(0, 0);
		intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		finish();
		
		overridePendingTransition(0, 0);
		startActivity(intent);
	}
}
